<template>
  <div class="login-bg">
    <div class="login-container">
      <h2 class="login-title">项目集管理系统</h2>
      <form @submit.prevent="onSubmit" class="login-form">
        <div class="form-group">
          <input v-model="username" id="username" type="text" placeholder="用户名" required autofocus />
        </div>
        <div class="form-group">
          <input v-model="password" id="password" type="password" placeholder="密码" required />
        </div>
        <div v-if="error" class="error-msg">{{ error }}</div>
        <button type="submit" class="login-btn" :disabled="loading">
          {{ loading ? '登录中...' : '登录' }}
        </button>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import axios from 'axios'

const username = ref('')
const password = ref('')
const error = ref('')
const loading = ref(false)

const onSubmit = async () => {
  error.value = ''
  loading.value = true
  try {
    const res = await axios.post('/api/login', {
      username: username.value,
      password: password.value
    })
    localStorage.setItem('token', res.data.token)
    window.location.href = '/'
  } catch (e) {
    error.value = e.response?.data?.detail || '登录失败'
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: #f5f6fa;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 0;
}
.login-container {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 16px 0 #e0e0e0;
  padding: 40px 36px 32px 36px;
  width: 400px;
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 2;
}
.login-title {
  color: #222;
  font-size: 2rem;
  font-weight: 600;
  margin-bottom: 28px;
  letter-spacing: 1px;
}
.login-form {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.form-group {
  display: flex;
  flex-direction: column;
  gap: 0;
}
.form-group input {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid #d1d5db;
  border-radius: 5px;
  background: #fafbfc;
  color: #222;
  font-size: 1rem;
  outline: none;
  transition: border 0.2s;
}
.form-group input:focus {
  border: 1.5px solid #409eff;
  background: #fff;
}
.login-btn {
  width: 100%;
  padding: 12px;
  background: #409eff;
  color: #fff;
  font-size: 1.1rem;
  font-weight: 500;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: background 0.2s;
}
.login-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}
.error-msg {
  color: #ff1744;
  background: #fff0f0;
  border-radius: 4px;
  padding: 6px 12px;
  font-size: 0.95rem;
  text-align: center;
  border: 1px solid #ffd6d6;
}
</style> 